package com.qiche.controller.wx;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.qiche.aspect.Result;
import com.qiche.enums.ResultEnum;
import com.qiche.model.StoreMember;
import com.qiche.model.StoreMemberCar;
import com.qiche.service.StoreMemberCarService;

/**
 * 微信用户车辆信息控制器
 */
@RestController
@RequestMapping("wx/memberCar")
public class MemberCarWxController {

    @Autowired
    private StoreMemberCarService storeMemberCarService;


    /**
     * 获取用户车辆信息列表
     * @return
     */
    @PostMapping("/getList")
    public Result<?> getList(@RequestParam String accessToken,@RequestParam String openid, String unionid,@RequestParam Long merchantId, StoreMember storeMember) {
        List<StoreMemberCar> memberCars = storeMemberCarService.getCarsByStoreMemberId(storeMember.getId());
        return  Result.createByResultEnum(ResultEnum.SUCCESS, memberCars);
    }



    /**
     * 保存用户车辆信息
     * @param memberCar
     * @param result
     * @return
     */
    @PostMapping("/save")
	public Result<?> saveMemberCar(@RequestParam String accessToken, @RequestParam String openid, String unionid,
			@RequestParam Long merchantId, StoreMember storeMember, @Validated StoreMemberCar storeMemberCar,
			BindingResult result) {
        if (result.hasErrors()) {
            return  Result.createByErrorCodeMessage(-1,result.getFieldError().getDefaultMessage());
        }
        storeMemberCar.setStoreMemberId(storeMember.getId());
        try {
                return storeMemberCarService.saveOrUpdate(storeMemberCar);
        } catch (Exception e) {
            e.printStackTrace();
            return  Result.createByResultEnum(ResultEnum.UNKONW_ERROR);
        }

    }
}
